package com.group3.service.impl;

import com.group3.dao.StudentInfoMapper;
import com.group3.dao.StudentMapper;
import com.group3.domain.Student;
import com.group3.domain.StudentExample;
import com.group3.domain.StudentInfo;
import com.group3.domain.StudentInfoExample;
import com.group3.pojo.receive.StudentReceive;
import com.group3.service.AStudentService;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-06-18 周五
 * @time 17:18
 * @description
 */
@Service
public class AStudentServiceImpl implements AStudentService {

    @Resource
    private StudentInfoMapper studentInfoMapper;

    @Resource
    private StudentMapper studentMapper;

    /**
     * @Description: 管理员修改学生信息
     * @Param: [studentId, studentReceive]
     * @return: com.group3.utils.Result
     * @Author: 王林
     * @Date: 2021/6/19
     */
    @Override
    @Transactional
    public Result updateStudentInfo(StudentReceive studentReceive) {

        //学生修改后的信息
        Student student = new Student(studentReceive);

        //修改学生的条件
        StudentExample studentExample = new StudentExample();
        studentExample.createCriteria().andStudentIdEqualTo(studentReceive.getBeforeStudentId());

        int rs = studentMapper.updateByExampleSelective(student, studentExample);

        return rs > 0 ? ResultUtil.success("修改成功") : ResultUtil.error("修改失败");
    }

    /**
     * @Description: 管理员删除学生信息
     * @Param: [studentIds]
     * @return: com.group3.utils.Result
     * @Author: 王林
     * @Date: 2021/6/19
     */
    @Override
    @Transactional
    public Result deleteStudent(List<String> studentIds) {
        //将学生从班级中移出，将该学生的班级id设为null
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setClassId(null);

        for (String studentId : studentIds) {
            StudentInfoExample studentInfoExample = new StudentInfoExample();
            studentInfoExample.createCriteria().andStudentIdEqualTo(studentId);

            int rs = studentInfoMapper.updateByExampleSelective(studentInfo, studentInfoExample);
            if (rs < 0) {
                return ResultUtil.error("删除失败");
            }
        }
        return ResultUtil.success("删除成功");
    }

    /**
     * 学生自己查询自己的信息（登录成功直接显示）
     */
    @Override
    @Transactional
    public Result<List<StudentInfo>> getInfoStudent(String studentId) {

        StudentInfoExample studentInfoExample = new StudentInfoExample();
        StudentInfoExample.Criteria criteria = studentInfoExample.createCriteria();
        criteria.andStudentIdEqualTo(studentId);//根据学生id获取请假数据
        /**
         * 装数据库自己的学生信息
         */
        List<StudentInfo> student = studentInfoMapper.selectByExample(studentInfoExample);

        return ResultUtil.success(student);
    }

}
